package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Shipping;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {
    @Reference
     private ShippingService shippingService;

    @Reference
    private PackingService packingService;


    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int pageSize){
        PageInfo<Shipping> pageInfo = shippingService.findByPage(pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/shipping/shipping-list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object>delete(String id){
        String msg = shippingService.delete(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",msg);
        return map;

    }
    @RequestMapping("/edit")
    public String edit(Shipping shipping) {

        shippingService.save(shipping);
        return "redirect:/cargo/shipping/list";
    }

    @RequestMapping("/toShipping")
    public String toShipping(String id) {


        //判断状态码是否为0 为0则执行去到委托添加页面 否则重新加载页面
        boolean pass = packingService.isPass(id);
        if (pass) {
            //获取数据并回写
            Shipping shipping = shippingService.findByPackingId(id);

            request.setAttribute("id", id);
            request.setAttribute("shipping", shipping);
            return "/cargo/shipping/shipping-toShipping";
        }

        return "redirect:/cargo/packing/list";
    }



    @RequestMapping("/print")
    public void print(String id)throws Exception{

        //1. 读取jasper文件流
        InputStream in =
                session.getServletContext()
                        .getResourceAsStream("/jasper/test.jasper");
        Shipping shipping = shippingService.findById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("shipper",shipping.getShipper());
        map.put("consignee",shipping.getConsignee());
        map.put("notifyParty",shipping.getNotifyParty());
        map.put("lcNo",shipping.getLcNo());
        map.put("portOfLoading",shipping.getPortOfLoading());
        map.put("portOfTrans",shipping.getPortOfTrans());
        map.put("portOfDischarge",shipping.getPortOfDischarge());
        map.put("loadingDate",shipping.getLoadingDate());
        map.put("limitDate",shipping.getLimitDate());
        map.put("specialCondition",shipping.getSpecialCondition());
        //2. 创建JasperPrint对象,用来往模板中填充数据
        // 参数1：jasper文件流
        // 参数2：通过map方式往模板填充数据;  目前传入空的map
        // 参数3：通过数据源往模板填充数据； 目前传入空的数据源
        JasperPrint jasperPrint = JasperFillManager.fillReport(in,map,new JREmptyDataSource());

        //3. 导出pdf
        // 参数1：JasperPrint对象 （模板 + 数据）
        // 参数2：response输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());

        in.close();

    }





}
